package com.jluzh10.learning.service.Impl;

import com.github.pagehelper.PageHelper;
import com.jluzh10.learning.dao.CommentMapper;
import com.jluzh10.learning.domain.Comment;
import com.jluzh10.learning.domain.CommentExample;
import com.jluzh10.learning.domain.Course;
import com.jluzh10.learning.service.CourseCommentService;
import com.jluzh10.learning.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class CourseCommentServiceImpl implements CourseCommentService {

    private final CommentMapper courseCommentMapper;
    private final CourseService courseService;

    @Autowired
    public CourseCommentServiceImpl(CommentMapper courseCommentMapper, CourseService courseService) {
        this.courseCommentMapper = courseCommentMapper;
        this.courseService = courseService;
    }

    @Override
    public void addScopeAndContent(Comment courseComment) throws Exception {
        //设置评论时间
        courseComment.setCreateTime(new Date());
        //插入新的评论
        courseCommentMapper.insertSelective(courseComment);
        //更新评论的课程评价信息
        courseService.updateScopeByComment(courseComment);
    }


    @Override
    public List<Comment> findComment(Integer courseId,Integer limit,Integer offset) {

        //分页
        PageHelper.startPage(offset,limit);
        return courseCommentMapper.findComment(courseId);
    }
}
